package com.heart.house.web.controller;

import com.heart.house.biz.service.BlogService;
import com.heart.house.biz.service.CommentService;
import com.heart.house.biz.service.RecommandService;
import com.heart.house.common.constants.CommonConstants;
import com.heart.house.common.model.Blog;
import com.heart.house.common.model.Comment;
import com.heart.house.common.model.House;
import com.heart.house.common.page.PageData;
import com.heart.house.common.page.PageParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @author Luo Zhen
 * @create 2019-06-28 15:11
 */
@Controller
public class BlogController {

    @Autowired
    private RecommandService recommandService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private CommentService commentService;

    /**
     * 博客评论列表
     * s
     *
     * @param pageSize
     * @param pageNum
     * @param query
     * @param modelMap
     * @return
     */
    @RequestMapping("/blog/list")
    public String list(Integer pageSize, Integer pageNum, Blog query, ModelMap modelMap) {
        PageData<Blog> ps = blogService.queryBlog(query, PageParams.build(pageSize, pageNum));
        List<House> houses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", houses);
        modelMap.put("ps", ps);
        return "blog/listing";
    }

    /**
     * 博客详情页
     *
     * @param id
     * @param modelMap
     * @return
     */
    @RequestMapping("blog/detail")
    public String detail(Integer id, ModelMap modelMap) {
        Blog blog = blogService.queryOneBlog(id);
        // 防止模拟用户详情请求跳转到主页
        if (blog == null) {
            return "redirect:list";
        }
        List<Comment> comments = commentService.getBlogComments(id);
        List<House> houses = recommandService.getHostList(CommonConstants.RECOM_SIZE);
        modelMap.put("recomHouses", houses);
        modelMap.put("blog", blog);
        modelMap.put("commentList", comments);
        return "blog/detail";

    }


}
